@using System.Net
@namespace Oqtane.Themes.Controls
@inherits ThemeControlBase
@inject NavigationManager NavigationManager
@inject IUserService UserService
@inject IModuleDefinitionService ModuleDefinitionService
@inject IThemeService ThemeService
@inject IModuleService ModuleService
@inject IPageService PageService
@inject IPageModuleService PageModuleService
@inject ILogService logger
@inject ISettingService SettingService
@inject IJSRuntime jsRuntime 
@inject IServiceProvider ServiceProvider
@inject IStringLocalizer<ControlPanel> Localizer
@inject IStringLocalizer<SharedResources> SharedLocalizer

@if (ShowLanguageSwitcher)
{
    <LanguageSwitcher DropdownAlignment="@LanguageDropdownAlignment" />
}

@if (_showEditMode || (PageState.Page.IsPersonalizable && PageState.User != null && UserSecurity.IsAuthorized(PageState.User, RoleNames.Registered)))
{
    if (PageState.EditMode)
    {
        <button type="button" class="btn @ButtonClass active" data-bs-toggle="button" aria-pressed="true" autocomplete="off" @onclick="(async () => await ToggleEditMode(PageState.EditMode))">
            <span class="oi oi-pencil"></span>
        </button>
    }
    else
    {
        <button type="button" class="btn @ButtonClass" data-bs-toggle="button" aria-pressed="false" autocomplete="off" @onclick="(async () => await ToggleEditMode(PageState.EditMode))">
            <span class="oi oi-pencil"></span>
        </button>
    }
}

@if (_canViewAdminDashboard || UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, PageState.Page.PermissionList))
{
    <button type="button" class="btn @ButtonClass ms-1" data-bs-toggle="offcanvas" data-bs-target="#offcanvasControlPanel" aria-controls="offcanvasControlPanel" @onclick="ClearMessage">
        <span class="oi oi-cog"></span>
    </button>

	<div class="@ContainerClass" tabindex="-1" data-bs-scroll="true" data-bs-backdrop="true" id="offcanvasControlPanel" aria-labelledby="offcanvasScrollingLabel">
		<div class="@HeaderClass">
			<h5 id="offcanvasScrollingLabel" class="offcanvas-title">@Localizer["ControlPanel"]</h5>
			<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="Close" @onclick="ClearMessage"></button>
		</div>
		<div class="@BodyClass">
			<div class="container-fluid">
				@if (_canViewAdminDashboard)
				{
					<div class="row d-flex">
						<div class="col">
							<button type="button" data-bs-dismiss="offcanvas" class="btn btn-primary col-12" @onclick=@(async () => Navigate("Admin"))>@Localizer["AdminDash"]</button>
						</div>
					</div>
					<hr class="app-rule" />
				}
                @if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, PageState.Page.PermissionList))
				{
					<div class="row">
						<div class="col text-center">
							<label class="control-label">@Localizer["Page.Manage"] </label>
						</div>
					</div>
					<div class="row d-flex mb-2">
						<div class="col d-flex justify-content-between">
                            @if (PageState.Page.UserId == null)
                            {
                                <button type="button" class="btn btn-secondary col me-1" data-bs-dismiss="offcanvas" @onclick=@(async () => Navigate("Add"))>@SharedLocalizer["Add"]</button>
                            }
							<button type="button" class="btn btn-secondary col" data-bs-dismiss="offcanvas" @onclick=@(async () => Navigate("Edit"))>@SharedLocalizer["Edit"]</button>
							<button type="button" class="btn btn-danger col ms-1" @onclick="ConfirmDelete">@SharedLocalizer["Delete"]</button>
						</div>
					</div>
					<div class="row d-flex">
						<div class="col">
							@if (UserSecurity.ContainsRole(PageState.Page.PermissionList, PermissionNames.View, RoleNames.Everyone))
							{
								<button type="button" class="btn btn-secondary col-12" @onclick=@(async () => Publish("unpublish"))>@Localizer["Page.Unpublish"]</button>
							}
							else
							{
								<button type="button" class="btn btn-secondary col-12" @onclick=@(async () => Publish("publish"))>@Localizer["Page.Publish"]</button>
							}
						</div>
					</div>
					<hr class="app-rule" />

					@if (_deleteConfirmation)
					{
						<div class="app-admin-modal">
							<div class="modal" tabindex="-1" role="dialog">
								<div class="modal-dialog">
									<div class="modal-content">
										<div class="modal-header">
											<h5 class="modal-title">@Localizer["Page.Delete"]</h5>
											<button type="button" class="btn-close" aria-label="Close" @onclick="ConfirmDelete"></button>
										</div>
										<div class="modal-body">
											<p>@Localizer["Confirm.Page.Delete"]</p>
										</div>
										<div class="modal-footer">
											<button type="button" class="btn btn-danger" @onclick="DeletePage">@SharedLocalizer["Delete"]</button>
											<button type="button" class="btn btn-secondary" @onclick="ConfirmDelete">@SharedLocalizer["Cancel"]</button>
										</div>
									</div>
								</div>
							</div>
						</div>
					}

					<div class="row">
						<div class="col text-center">
							<label for="Module" class="control-label">@Localizer["Module.Manage"]</label>
							<select class="form-select" @bind="@ModuleType">
								<option value="new">@Localizer["Module.AddNew"]</option>
								<option value="existing">@Localizer["Module.AddExisting"]</option>
							</select>
							@if (ModuleType == "new")
							{
								@if (_moduleDefinitions != null)
								{
									<select class="form-select mt-1" @onchange="(e => CategoryChanged(e))">
										@foreach (var category in _categories)
										{
											if (category == Category)
											{
												<option value="@category" selected>@category @Localizer["Modules"]</option>
											}
											else
											{
												<option value="@category">@category @Localizer["Modules"]</option>
											}
										}
									</select>
                                    <select class="form-select mt-1" @onchange="(e => ModuleChanged(e))">
										@if (ModuleDefinitionName == "-")
										{
											<option value="-" selected>&lt;@Localizer["Module.Select"]&gt;</option>
										}
										else
										{
											<option value="-">&lt;@Localizer["Module.Select"]&gt;</option>
										}
										@foreach (var moduledefinition in _moduleDefinitions)
										{
											if (moduledefinition.IsEnabled && UserSecurity.IsAuthorized(PageState.User, PermissionNames.Utilize, moduledefinition.PermissionList))
											{
												if (moduledefinition.Runtimes == "" || moduledefinition.Runtimes.Contains(PageState.Runtime.ToString()))
												{
													<option value="@moduledefinition.ModuleDefinitionName">@moduledefinition.Name</option>
												}
											}
										}
									</select>
								}
							}
							else
							{
                                <select class="form-select mt-1" @onchange="(e => PageChanged(e))">
									<option value="-">&lt;@Localizer["Page.Select"]&gt;</option>
									@foreach (Page p in _pages)
									{
										<option value="@p.PageId">@p.Name</option>
									}
								</select>
                                <select class="form-select mt-1" @bind="@ModuleId">
									<option value="-">&lt;@Localizer["Module.Select"]&gt;</option>
									@foreach (Module module in _modules)
									{
										<option value="@module.ModuleId">@module.Title</option>
									}
								</select>
							}
						</div>
					</div>
					<div class="row">
						<div class="col text-center">
							<label for="Title" class="control-label">@Localizer["Title"]</label>
							<input type="text" name="Title" class="form-control" @bind="@Title" />
						</div>
					</div>
					<div class="row">
						<div class="col text-center">
							<label for="Pane" class="control-label">@Localizer["Pane"]</label>
							<select class="form-select" @bind="@Pane">
								@foreach (string pane in PageState.Page.Panes)
								{
									<option value="@pane">@pane Pane</option>
								}
							</select>
						</div>
					</div>
                    <div class="row">
                        <div class="col text-center">
                            <label for="Insert" class="control-label">@Localizer["Location"]</label>
                            <select class="form-select" @bind="@Location">
                                <option value="@int.MinValue">@Localizer["LocationTop"]</option>
                                <option value="@int.MaxValue">@Localizer["LocationBottom"]</option>
                            </select>
                        </div>
                    </div>
					<div class="row">
						<div class="col text-center">
							<label for="Container" class="control-label">@Localizer["Container"]</label>
							<select class="form-select" @bind="@ContainerType">
								@foreach (var container in _containers)
								{
									<option value="@container.TypeName">@container.Name</option>
								}
							</select>
						</div>
					</div>
					<div class="row">
						<div class="col text-center">
							<label for="visibility" class="control-label">@Localizer["Visibility"]</label>
							<select class="form-select" @bind="@Visibility">
								<option value="view">@Localizer["VisibilityView"]</option>
								<option value="edit">@Localizer["VisibilityEdit"]</option>
							</select>
						</div>
					</div>
					<button type="button" class="btn btn-primary col-12 mt-4" @onclick="@AddModule">@Localizer["Page.Module.Add"]</button>
					@((MarkupString)Message)
                    <hr class="app-rule" />
                }

                <div class="row d-flex">
                    <div class="col">
                        <button type="button" data-bs-dismiss="offcanvas" class="btn btn-secondary col-12" @onclick=@(async () => await LogoutUser())>@Localizer["Logout"]</button>
                    </div>
                </div>
            </div>
		</div>
	</div>
}

@code{
    [Parameter]
    public string ButtonClass { get; set; } = "btn-outline-secondary";

    [Parameter]
    public string ContainerClass { get; set; } = "offcanvas offcanvas-end";

    [Parameter]
    public string HeaderClass { get; set; } = "offcanvas-header";

    [Parameter]
    public string BodyClass { get; set; } = "offcanvas-body overflow-auto";

    [Parameter]
    public bool ShowLanguageSwitcher { get; set; } = true;

    [Parameter]
    public string LanguageDropdownAlignment { get; set; } = string.Empty; // Empty or Left or Right

    private bool _canViewAdminDashboard = false;
    private bool _showEditMode = false;
    private bool _deleteConfirmation = false;
    private List<string> _categories = new List<string>();
    private List<ModuleDefinition> _allModuleDefinitions;
    private List<ModuleDefinition> _moduleDefinitions;
    private List<Page> _pages = new List<Page>();
    private List<Module> _modules = new List<Module>();
    private List<ThemeControl> _containers = new List<ThemeControl>();
    private string _category = "Common";
    private string _pane = "";

    protected string PageId { get; private set; } = "-";
    protected string ModuleId { get; private set; } = "-";
    protected string ModuleType { get; private set; } = "new";
    protected string ModuleDefinitionName { get; private set; } = "-";

    protected string Title { get; private set; } = "";
    protected string ContainerType { get; private set; } = "";
    protected int Location { get; private set; } = int.MaxValue;
    protected string Visibility { get; private set; } = "view";
    protected string Message { get; private set; } = "";

    private string settingCategory = "CP-category";
    private string settingPane = "CP-pane";

    protected string Category
    {
        get => _category;
        private set
        {
            if (_category != value)
            {
                _category = value;
                _moduleDefinitions = _allModuleDefinitions.Where(item => item.Categories.Contains(Category)).ToList();
                ModuleDefinitionName = "-";
                Message = "";
                StateHasChanged();
                _ = UpdateSettingsAsync();
            }
        }
    }

    protected string Pane
    {
        get => _pane;
        private set
        {
            if (_pane != value)
            {
                _pane = value;
                _ = UpdateSettingsAsync();
            }
        }
    }

    protected override async Task OnParametersSetAsync()
    {
        _canViewAdminDashboard = CanViewAdminDashboard();
        _showEditMode = false;
        if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, PageState.Page.PermissionList))
        {
            _showEditMode = true;
            LoadSettingsAsync();

            _pages?.Clear();
            foreach (Page p in PageState.Pages)
            {
                if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, p.PermissionList))
                {
                    _pages.Add(p);
                }
            }

            var themes = await ThemeService.GetThemesAsync();
            _containers = ThemeService.GetContainerControls(themes, PageState.Page.ThemeType);
            ContainerType = PageState.Site.DefaultContainerType;
            _allModuleDefinitions = await ModuleDefinitionService.GetModuleDefinitionsAsync(PageState.Site.SiteId);
            _moduleDefinitions = _allModuleDefinitions.Where(item => item.Categories.Contains(Category)).ToList();
            _categories = _allModuleDefinitions.SelectMany(m => m.Categories.Split(',')).Distinct().ToList();
        }
        else
        {
            foreach (var module in PageState.Modules.Where(item => item.PageId == PageState.Page.PageId))
            {
                if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, module.PermissionList))
                {
                    _showEditMode = true;
                    break;
                }
            }			
        }
    }

    private bool CanViewAdminDashboard()
    {
        var admin = PageState.Pages.FirstOrDefault(item => item.Path == "admin");
        if (admin != null)
        {
            foreach (var page in PageState.Pages.Where(item => item.ParentId == admin?.PageId))
            {
                if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, page.PermissionList))
                {
                    return true;
                }
            }
        }
        return false;
    }

    private void CategoryChanged(ChangeEventArgs e)
    {
        Category = (string)e.Value;
    }

    private void ModuleChanged(ChangeEventArgs e)
    {
        ModuleDefinitionName = (string)e.Value;
        if (ModuleDefinitionName != "-")
        {
            var moduleDefinition = _moduleDefinitions.FirstOrDefault(item => item.ModuleDefinitionName == ModuleDefinitionName);
            Message = "<div class=\"alert alert-info mt-2 text-center\" role=\"alert\">" + moduleDefinition.Description + "</div>";
        }
        else
        {
            Message = "";
        }
        StateHasChanged();
    }

    private void PageChanged(ChangeEventArgs e)
    {
        PageId = (string)e.Value;
        if (PageId != "-")
        {
            _modules = PageState.Modules
                .Where(module => module.PageId == int.Parse(PageId) &&
                UserSecurity.IsAuthorized(PageState.User, PermissionNames.View, module.PermissionList))
                .ToList();
        }
        ModuleId = "-";
        StateHasChanged();
    }

    private async Task AddModule()
    {
        if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, PageState.Page.PermissionList))
        {
            if ((ModuleType == "new" && ModuleDefinitionName != "-") || (ModuleType != "new" && ModuleId != "-"))
            {
                if (ModuleType == "new")
                {
                    Module module = new Module();
                    module.SiteId = PageState.Site.SiteId;
                    module.PageId = PageState.Page.PageId;
                    module.ModuleDefinitionName = ModuleDefinitionName;
                    module.AllPages = false;

                    var permissions = new List<Permission>();
                    if (Visibility == "view")
                    {
                        // set module view permissions to page view permissions
                        permissions = SetPermissions(permissions, module.SiteId, PermissionNames.View, PermissionNames.View);
                    }
                    else
                    {
                        // set module view permissions to page edit permissions
                        permissions = SetPermissions(permissions, module.SiteId, PermissionNames.View, PermissionNames.Edit);
                    }
                    // set module edit permissions to page edit permissions
                    permissions = SetPermissions(permissions, module.SiteId, PermissionNames.Edit, PermissionNames.Edit);
                    module.PermissionList = permissions;

                    module = await ModuleService.AddModuleAsync(module);
                    ModuleId = module.ModuleId.ToString();
                }

                var pageModule = new PageModule
                {
                    PageId = PageState.Page.PageId,
                    ModuleId = int.Parse(ModuleId),
                    Title = Title
                };
                if (pageModule.Title == "")
                {
                    if (ModuleType == "new")
                    {
                        pageModule.Title = _moduleDefinitions.FirstOrDefault(item => item.ModuleDefinitionName == ModuleDefinitionName)?.Name;
                    }
                    else
                    {
                        pageModule.Title = _modules.FirstOrDefault(item => item.ModuleId == int.Parse(ModuleId))?.Title;
                    }
                }

                pageModule.Pane = Pane;
                pageModule.Order = Location;
                pageModule.ContainerType = ContainerType;

                if (pageModule.ContainerType == PageState.Site.DefaultContainerType)
                {
                    pageModule.ContainerType = "";
                }

                await PageModuleService.AddPageModuleAsync(pageModule);
                await PageModuleService.UpdatePageModuleOrderAsync(pageModule.PageId, pageModule.Pane);

                Message = $"<div class=\"alert alert-success mt-2 text-center\" role=\"alert\">{Localizer["Success.Page.ModuleAdd"]}</div>";
                Title = "";
                NavigationManager.NavigateTo(NavigateUrl());
            }
            else
            {
                Message = $"<div class=\"alert alert-warning mt-2 text-center\" role=\"alert\">{Localizer["Message.Require.ModuleSelect"]}</div>";
            }
        }
        else
        {
            Message = $"<div class=\"alert alert-error mt-2 text-center\" role=\"alert\">{Localizer["Error.Authorize.No"]}</div>";
        }
    }

    private List<Permission> SetPermissions(List<Permission> permissions, int siteId, string modulePermission, string pagePermission)
    {
        foreach (var permission in PageState.Page.PermissionList.Where(item => item.PermissionName == pagePermission))
        {
            permissions.Add(new Permission { SiteId = siteId, EntityName = EntityNames.Module, PermissionName = modulePermission, RoleId = permission.RoleId, UserId = permission.UserId, IsAuthorized = permission.IsAuthorized });
        }
        return permissions;
    }

    private async Task ToggleEditMode(bool EditMode)
    {
        Page page = null;
        if (PageState.Page.IsPersonalizable && PageState.User != null && UserSecurity.IsAuthorized(PageState.User, RoleNames.Registered))
        {
            page = await PageService.AddPageAsync(PageState.Page.PageId, PageState.User.UserId);
        }

        if (_showEditMode)
        {
            if (EditMode)
            {
                PageState.EditMode = false;
            }
            else
            {
                PageState.EditMode = true;
            }

            // preserve other querystring parameters
            if (PageState.QueryString.ContainsKey("edit")) PageState.QueryString.Remove("edit");
            PageState.QueryString.Add("edit", PageState.EditMode.ToString().ToLower());
            var url = PageState.Route.AbsolutePath + Utilities.CreateQueryString(PageState.QueryString);
            NavigationManager.NavigateTo(url);
		}
		else
		{
            if (PageState.Page.IsPersonalizable && PageState.User != null && UserSecurity.IsAuthorized(PageState.User, RoleNames.Registered))
			{
				PageState.EditMode = true;
				NavigationManager.NavigateTo(NavigateUrl(page.Path, "edit=" + ((PageState.EditMode) ? "true" : "false")));
			}
		}
	}

	private void Navigate(string location)
	{
		Module module;
		switch (location)
		{
			case "Admin":
			    // get admin dashboard moduleid
				module = PageState.Modules.FirstOrDefault(item => item.ModuleDefinitionName == Constants.AdminDashboardModule);
				if (module != null)
				{
                    NavigationManager.NavigateTo(EditUrl("admin", module.ModuleId, "Index", "returnurl=" + WebUtility.UrlEncode(PageState.Route.PathAndQuery)));
				}
				break;
			case "Add":
			case "Edit":
				string url = "";
				// get page management moduleid
				module = PageState.Modules.FirstOrDefault(item => item.ModuleDefinitionName == Constants.PageManagementModule);

				if (module != null)
				{
					switch (location)
					{
						case "Add":
                            url = EditUrl("admin/pages", module.ModuleId, location, $"id={PageState.Page.PageId}&returnurl={WebUtility.UrlEncode(PageState.Route.PathAndQuery)}");
                            break;
						case "Edit":
                            url = EditUrl("admin/pages", module.ModuleId, location, $"id={PageState.Page.PageId}&returnurl={WebUtility.UrlEncode(PageState.Route.PathAndQuery)}");
							break;
					}
				}

				if (url != "")
				{
					NavigationManager.NavigateTo(url);
				}

				break;
		}
	}

	private async void Publish(string action)
	{
		if (UserSecurity.IsAuthorized(PageState.User, PermissionNames.Edit, PageState.Page.PermissionList))
		{
			var permissions = PageState.Page.PermissionList;
            switch (action)
            {
                case "publish":
                    if (!permissions.Any(item => item.PermissionName == PermissionNames.View && item.RoleName == RoleNames.Everyone))
                    {
                        permissions.Add(new Permission(PageState.Site.SiteId, EntityNames.Page, PageState.Page.PageId, PermissionNames.View, RoleNames.Everyone, null, true));
                    }
                    if (!permissions.Any(item => item.PermissionName == PermissionNames.View && item.RoleName == RoleNames.Registered))
                    {
                        permissions.Add(new Permission(PageState.Site.SiteId, EntityNames.Page, PageState.Page.PageId, PermissionNames.View, RoleNames.Registered, null, true));
                    }
                    break;
                case "unpublish":
                    if (permissions.Any(item => item.PermissionName == PermissionNames.View && item.RoleName == RoleNames.Everyone))
                    {
                        permissions.RemoveAll(item => item.PermissionName == PermissionNames.View && item.RoleName == RoleNames.Everyone);
                    }

                    if (permissions.Any(item => item.PermissionName == PermissionNames.View && item.RoleName == RoleNames.Registered))
                    {
                        permissions.RemoveAll(item => item.PermissionName == PermissionNames.View && item.RoleName == RoleNames.Registered);
                    }
                    break;
            }
			PageState.Page.PermissionList = permissions;
			await PageService.UpdatePageAsync(PageState.Page);
			NavigationManager.NavigateTo(NavigateUrl(PageState.Page.Path, true));
		}
	}

	private void ConfirmDelete()
	{
		_deleteConfirmation = !_deleteConfirmation;
		StateHasChanged();
	}

	private async Task DeletePage()
	{
		ConfirmDelete();

		var page = PageState.Page;
		try
		{
			if (page.UserId == null)
			{
				page.IsDeleted = true;
				await PageService.UpdatePageAsync(page);
				await logger.Log(page.PageId, null, PageState.User?.UserId, GetType().AssemblyQualifiedName, "ControlPanel", LogFunction.Delete, LogLevel.Information, null, "Page Deleted {Page}", page);
				NavigationManager.NavigateTo(NavigateUrl(""));
			}
			else // personalized page
			{
				await PageService.DeletePageAsync(page.PageId);
				await logger.Log(page.PageId, null, PageState.User?.UserId, GetType().AssemblyQualifiedName, "ControlPanel", LogFunction.Delete, LogLevel.Information, null, "Page Deleted {Page}", page);
				NavigationManager.NavigateTo(NavigateUrl());
			}
		}
		catch (Exception ex)
		{
			await logger.Log(page.PageId, null, PageState.User?.UserId, GetType().AssemblyQualifiedName, "ControlPanel", LogFunction.Delete, LogLevel.Information, ex, "Page Deleted {Page} {Error}", page, ex.Message);
		}
	}

    // the following code is duplicated from LoginBase
    private async Task LogoutUser()
    {
        await LoggingService.Log(PageState.Alias, PageState.Page.PageId, null, PageState.User?.UserId, GetType().AssemblyQualifiedName, "Logout", LogFunction.Security, LogLevel.Information, null, "User Logout For Username {Username}", PageState.User?.Username);

        Route route = new Route(PageState.Uri.AbsoluteUri, PageState.Alias.Path);
        var url = route.PathAndQuery;

        // verify if anonymous users can access page
        if (!UserSecurity.IsAuthorized(null, PermissionNames.View, PageState.Page.PermissionList))
        {
            url = PageState.Alias.Path;
        }

        if (PageState.Runtime == Shared.Runtime.Hybrid)
        {
            // hybrid apps utilize an interactive logout
            await UserService.LogoutUserAsync(PageState.User);
            var authstateprovider = (IdentityAuthenticationStateProvider)ServiceProvider.GetService(typeof(IdentityAuthenticationStateProvider));
            authstateprovider.NotifyAuthenticationChanged();
            NavigationManager.NavigateTo(url, true);
        }
        else
        {
            // post to the Logout page to complete the logout process
            var fields = new { __RequestVerificationToken = SiteState.AntiForgeryToken, returnurl = url };
            var interop = new Interop(jsRuntime);
            await interop.SubmitForm(Utilities.TenantUrl(PageState.Alias, "/pages/logout/"), fields);
        }
    }

	private void LoadSettingsAsync()
	{
        _category = SettingService.GetSetting(PageState.User.Settings, settingCategory, "Common");
        var pane = SettingService.GetSetting(PageState.User.Settings, settingPane, "");
		if (PageState.Page.Panes.Contains(pane))
		{
			_pane = pane;
		}
		else
		{
			if (PageState.Page.Panes.FindIndex(item => item.Equals(PaneNames.Default, StringComparison.OrdinalIgnoreCase)) != -1)
			{
				_pane = PaneNames.Default;
			}
			else
			{
				_pane = PaneNames.Admin;
			}
		}
    }

    private async Task UpdateSettingsAsync()
    {
        Dictionary<string, string> settings = await SettingService.GetUserSettingsAsync(PageState.User.UserId);
		settings = SettingService.SetSetting(settings, settingCategory, _category);
        settings = SettingService.SetSetting(settings, settingPane, _pane);
        await SettingService.UpdateUserSettingsAsync(settings, PageState.User.UserId);
    }

    private void ClearMessage()
    {

        Message = "";

    }
}
